package com.wxsoft.java.demo05.jackson;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonParse {

	public static void main(String[] args) throws Exception {
		String fileName = "E:\\workspace_eclipse_luna\\demo\\src\\main\\java\\com\\wxsoft\\java\\demo05\\jackson\\jsonString.txt";
		BufferedReader in = new BufferedReader(new FileReader(fileName));
		StringBuffer str = new StringBuffer("");
		String line = null;
		while ((line = in.readLine()) != null) {
			str.append(line);
		}
		in.close();
		String json = str.toString();
		
		Class<MemberEntity> clazz = MemberEntity.class;

		MemberEntity  m = (MemberEntity)jsonToObject(json, MemberEntity.class);
	}

	public static String objectToJson(Object obj)
			throws JsonProcessingException {
		// 定义转json的类
		ObjectMapper objectMapper = new ObjectMapper();
		// 将传入的对象转成json类型的字符串
		String json = objectMapper.writeValueAsString(obj);
		return json;
	}

	public static <T> Object jsonToObject(String json, Class<T> t)
			throws JsonParseException, JsonMappingException, IOException {
		// 定义转json的类
		ObjectMapper objectMapper = new ObjectMapper();
		T obj = objectMapper.readValue(json, t);
		return obj;
	}

}
